<template>
    <div class="goods-list">
        <!-- 在网页中有两种跳转方式： -->
        <!-- 方式1：使用a标签的形式叫做标签跳转 -->
        <!-- 方式2：使用window.location.href的形式，叫做编程式导航 -->
        <!-- <div class="goods-item" v-for="item in list" :key="item.id">
            <router-link :to="'/home/goodsinfo/'+item.id">
                <img :src="item.img_url" alt="">
                <h1 class="title">{{item.title}}</h1>
                <div class="info">
                    <p class="price">
                        <span class="now">￥{{item.sell_price}}</span>
                        <span class="old">￥{{item.market_price}}</span>
                    </p>
                    <p class="sell">
                        <span>热卖中</span><span>剩{{item.stock_quantity}}件</span>
                    </p>
                </div>
            </router-link>
        </div>        -->
        <div class="goods-item" v-for="item in list" :key="item.id" @click="getData(item.id)">
                <img :src="item.img_url" alt="">
                <h1 class="title">{{item.title}}</h1>
                <div class="info">
                    <p class="price">
                        <span class="now">￥{{item.sell_price}}</span>
                        <span class="old">￥{{item.market_price}}</span>
                    </p>
                    <p class="sell">
                        <span>热卖中</span><span>剩{{item.stock_quantity}}件</span>
                    </p>
                </div>
        </div>



        <mt-button type="danger" size="large" @click="getMore" v-show="showBtn">加载更多</mt-button>
    </div>
</template>
<style lang="scss" scoped>
.goods-list{
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    padding:5px;
    .goods-item{
        width: 49%;
        border:1px solid #ccc;
        box-shadow: 0 0 8px #ccc;
        margin: 4px 0;
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        min-height: 293px;
        img{
            width: 100%
        }
        .title{
            font-size:14px;
            padding: 0 5px
        }
        .info{
            background: #eee;
            p{
                margin: 0;
                padding: 5px;
            }
            .price{
                .now{
                    color:red;
                    font-weight: bold;
                    font-size: 16px;
                }
                .old{
                    text-decoration: line-through;
                    font-size:12px;
                    margin-left:10px
                }
            }
            .sell{
                display: flex;
                justify-content: space-between;
                font-size:12px;
            }
        }
    }
}
</style>
<script>
export default {
    data(){
        return{
            list:[],
            page:1,
            total:0,
            showBtn:true
        }
    },
    methods:{
        getGoodsList(){
            var that = this;            
            that.$http.get('../../data/goodslist.json').then(res=>{
                console.log(res.body)
                if(res.body.code==200){
                    var resList = res.body.list;
                    resList.forEach(resInner=>{
                        if(that.page == resInner.page){
                            var a = resInner.sub_list;
                            var b = that.list;
                            that.list = a.concat(b)
                            that.total += parseInt(resInner.sub_list.length) 
                        }
                    })
                    console.log(that.total)
                    if(that.total >= res.body.total){
                        that.showBtn = false
                    }
                }
            })
        },
        getMore(){
            this.page++;
            this.getGoodsList()
        },
        getData(id){
            // $route 获取路由中的参数对象，params、query都属于它；
            // $trouter 路由的导航对象，用它可以方便使用JS代码，实现路由的前进后退，跳转新的URL地址
            this.$router.push('/home/goodsinfo/'+id)


            // // 字符串
            // this.$router.push('home')
            // 对象
            // this.$router.push({ path: 'home' })
            // 命名的路由
            // this.$router.push({ name: 'user', params: { userId: 123 }})
            // 带查询参数，变成 /register?plan=123
            // this.$router.push({ path: 'register', query: { plan: '123' }})

            // push方法其实和<router-link :to="...">是等同的。
            // 注意：push方法的跳转会向 history 栈添加一个新的记录，当我们点击浏览器的返回按钮时可以看到之前的页面。
        }
    },
    created(){
        this.getGoodsList()
    }
}
</script>